package com.ugoodtech.mdcc.core.model.mapper;

import static org.apache.ibatis.jdbc.SqlBuilder.BEGIN;
import static org.apache.ibatis.jdbc.SqlBuilder.DELETE_FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.FROM;
import static org.apache.ibatis.jdbc.SqlBuilder.INSERT_INTO;
import static org.apache.ibatis.jdbc.SqlBuilder.ORDER_BY;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT;
import static org.apache.ibatis.jdbc.SqlBuilder.SELECT_DISTINCT;
import static org.apache.ibatis.jdbc.SqlBuilder.SET;
import static org.apache.ibatis.jdbc.SqlBuilder.SQL;
import static org.apache.ibatis.jdbc.SqlBuilder.UPDATE;
import static org.apache.ibatis.jdbc.SqlBuilder.VALUES;
import static org.apache.ibatis.jdbc.SqlBuilder.WHERE;

import com.ugoodtech.mdcc.core.model.entity.Users;
import com.ugoodtech.mdcc.core.model.entity.UsersExample.Criteria;
import com.ugoodtech.mdcc.core.model.entity.UsersExample.Criterion;
import com.ugoodtech.mdcc.core.model.entity.UsersExample;
import java.util.List;
import java.util.Map;

public class UsersSqlProvider {

    public String countByExample(UsersExample example) {
        BEGIN();
        SELECT("count(*)");
        FROM("users");
        applyWhere(example, false);
        return SQL();
    }

    public String deleteByExample(UsersExample example) {
        BEGIN();
        DELETE_FROM("users");
        applyWhere(example, false);
        return SQL();
    }

    public String insertSelective(Users record) {
        BEGIN();
        INSERT_INTO("users");
        
        if (record.getId() != null) {
            VALUES("id", "#{id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            VALUES("creation_time", "#{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            VALUES("deleted", "#{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            VALUES("enabled", "#{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            VALUES("update_time", "#{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAccountEnabled() != null) {
            VALUES("account_enabled", "#{accountEnabled,jdbcType=BIT}");
        }
        
        if (record.getAccountExpired() != null) {
            VALUES("account_expired", "#{accountExpired,jdbcType=BIT}");
        }
        
        if (record.getAccountLocked() != null) {
            VALUES("account_locked", "#{accountLocked,jdbcType=BIT}");
        }
        
        if (record.getCredentialsExpired() != null) {
            VALUES("credentials_expired", "#{credentialsExpired,jdbcType=BIT}");
        }
        
        if (record.getOpenid() != null) {
            VALUES("openId", "#{openid,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            VALUES("password", "#{password,jdbcType=VARCHAR}");
        }
        
        if (record.getServicenum() != null) {
            VALUES("serviceNum", "#{servicenum,jdbcType=INTEGER}");
        }
        
        if (record.getStationId() != null) {
            VALUES("station_id", "#{stationId,jdbcType=BIGINT}");
        }
        
        if (record.getUsertype() != null) {
            VALUES("userType", "#{usertype,jdbcType=INTEGER}");
        }
        
        if (record.getUsername() != null) {
            VALUES("username", "#{username,jdbcType=VARCHAR}");
        }
        
        if (record.getRole() != null) {
            VALUES("role", "#{role,jdbcType=INTEGER}");
        }
        
        if (record.getUserFlag() != null) {
            VALUES("user_flag", "#{userFlag,jdbcType=INTEGER}");
        }
        
        if (record.getUnionId() != null) {
            VALUES("union_id", "#{unionId,jdbcType=VARCHAR}");
        }
        
        return SQL();
    }

    public String selectByExample(UsersExample example) {
        BEGIN();
        if (example != null && example.isDistinct()) {
            SELECT_DISTINCT("id");
        } else {
            SELECT("id");
        }
        SELECT("creation_time");
        SELECT("deleted");
        SELECT("enabled");
        SELECT("update_time");
        SELECT("account_enabled");
        SELECT("account_expired");
        SELECT("account_locked");
        SELECT("credentials_expired");
        SELECT("openId");
        SELECT("password");
        SELECT("serviceNum");
        SELECT("station_id");
        SELECT("userType");
        SELECT("username");
        SELECT("role");
        SELECT("user_flag");
        SELECT("union_id");
        FROM("users");
        applyWhere(example, false);
        
        if (example != null && example.getOrderByClause() != null) {
            ORDER_BY(example.getOrderByClause());
        }
        
        return SQL();
    }

    public String updateByExampleSelective(Map<String, Object> parameter) {
        Users record = (Users) parameter.get("record");
        UsersExample example = (UsersExample) parameter.get("example");
        
        BEGIN();
        UPDATE("users");
        
        if (record.getId() != null) {
            SET("id = #{record.id,jdbcType=BIGINT}");
        }
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{record.deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{record.enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAccountEnabled() != null) {
            SET("account_enabled = #{record.accountEnabled,jdbcType=BIT}");
        }
        
        if (record.getAccountExpired() != null) {
            SET("account_expired = #{record.accountExpired,jdbcType=BIT}");
        }
        
        if (record.getAccountLocked() != null) {
            SET("account_locked = #{record.accountLocked,jdbcType=BIT}");
        }
        
        if (record.getCredentialsExpired() != null) {
            SET("credentials_expired = #{record.credentialsExpired,jdbcType=BIT}");
        }
        
        if (record.getOpenid() != null) {
            SET("openId = #{record.openid,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            SET("password = #{record.password,jdbcType=VARCHAR}");
        }
        
        if (record.getServicenum() != null) {
            SET("serviceNum = #{record.servicenum,jdbcType=INTEGER}");
        }
        
        if (record.getStationId() != null) {
            SET("station_id = #{record.stationId,jdbcType=BIGINT}");
        }
        
        if (record.getUsertype() != null) {
            SET("userType = #{record.usertype,jdbcType=INTEGER}");
        }
        
        if (record.getUsername() != null) {
            SET("username = #{record.username,jdbcType=VARCHAR}");
        }
        
        if (record.getRole() != null) {
            SET("role = #{record.role,jdbcType=INTEGER}");
        }
        
        if (record.getUserFlag() != null) {
            SET("user_flag = #{record.userFlag,jdbcType=INTEGER}");
        }
        
        if (record.getUnionId() != null) {
            SET("union_id = #{record.unionId,jdbcType=VARCHAR}");
        }
        
        applyWhere(example, true);
        return SQL();
    }

    public String updateByExample(Map<String, Object> parameter) {
        BEGIN();
        UPDATE("users");
        
        SET("id = #{record.id,jdbcType=BIGINT}");
        SET("creation_time = #{record.creationTime,jdbcType=TIMESTAMP}");
        SET("deleted = #{record.deleted,jdbcType=BIT}");
        SET("enabled = #{record.enabled,jdbcType=BIT}");
        SET("update_time = #{record.updateTime,jdbcType=TIMESTAMP}");
        SET("account_enabled = #{record.accountEnabled,jdbcType=BIT}");
        SET("account_expired = #{record.accountExpired,jdbcType=BIT}");
        SET("account_locked = #{record.accountLocked,jdbcType=BIT}");
        SET("credentials_expired = #{record.credentialsExpired,jdbcType=BIT}");
        SET("openId = #{record.openid,jdbcType=VARCHAR}");
        SET("password = #{record.password,jdbcType=VARCHAR}");
        SET("serviceNum = #{record.servicenum,jdbcType=INTEGER}");
        SET("station_id = #{record.stationId,jdbcType=BIGINT}");
        SET("userType = #{record.usertype,jdbcType=INTEGER}");
        SET("username = #{record.username,jdbcType=VARCHAR}");
        SET("role = #{record.role,jdbcType=INTEGER}");
        SET("user_flag = #{record.userFlag,jdbcType=INTEGER}");
        SET("union_id = #{record.unionId,jdbcType=VARCHAR}");
        
        UsersExample example = (UsersExample) parameter.get("example");
        applyWhere(example, true);
        return SQL();
    }

    public String updateByPrimaryKeySelective(Users record) {
        BEGIN();
        UPDATE("users");
        
        if (record.getCreationTime() != null) {
            SET("creation_time = #{creationTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getDeleted() != null) {
            SET("deleted = #{deleted,jdbcType=BIT}");
        }
        
        if (record.getEnabled() != null) {
            SET("enabled = #{enabled,jdbcType=BIT}");
        }
        
        if (record.getUpdateTime() != null) {
            SET("update_time = #{updateTime,jdbcType=TIMESTAMP}");
        }
        
        if (record.getAccountEnabled() != null) {
            SET("account_enabled = #{accountEnabled,jdbcType=BIT}");
        }
        
        if (record.getAccountExpired() != null) {
            SET("account_expired = #{accountExpired,jdbcType=BIT}");
        }
        
        if (record.getAccountLocked() != null) {
            SET("account_locked = #{accountLocked,jdbcType=BIT}");
        }
        
        if (record.getCredentialsExpired() != null) {
            SET("credentials_expired = #{credentialsExpired,jdbcType=BIT}");
        }
        
        if (record.getOpenid() != null) {
            SET("openId = #{openid,jdbcType=VARCHAR}");
        }
        
        if (record.getPassword() != null) {
            SET("password = #{password,jdbcType=VARCHAR}");
        }
        
        if (record.getServicenum() != null) {
            SET("serviceNum = #{servicenum,jdbcType=INTEGER}");
        }
        
        if (record.getStationId() != null) {
            SET("station_id = #{stationId,jdbcType=BIGINT}");
        }
        
        if (record.getUsertype() != null) {
            SET("userType = #{usertype,jdbcType=INTEGER}");
        }
        
        if (record.getUsername() != null) {
            SET("username = #{username,jdbcType=VARCHAR}");
        }
        
        if (record.getRole() != null) {
            SET("role = #{role,jdbcType=INTEGER}");
        }
        
        if (record.getUserFlag() != null) {
            SET("user_flag = #{userFlag,jdbcType=INTEGER}");
        }
        
        if (record.getUnionId() != null) {
            SET("union_id = #{unionId,jdbcType=VARCHAR}");
        }
        
        WHERE("id = #{id,jdbcType=BIGINT}");
        
        return SQL();
    }

    protected void applyWhere(UsersExample example, boolean includeExamplePhrase) {
        if (example == null) {
            return;
        }
        
        String parmPhrase1;
        String parmPhrase1_th;
        String parmPhrase2;
        String parmPhrase2_th;
        String parmPhrase3;
        String parmPhrase3_th;
        if (includeExamplePhrase) {
            parmPhrase1 = "%s #{example.oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{example.oredCriteria[%d].allCriteria[%d].value} and #{example.oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{example.oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{example.oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{example.oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{example.oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        } else {
            parmPhrase1 = "%s #{oredCriteria[%d].allCriteria[%d].value}";
            parmPhrase1_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s}";
            parmPhrase2 = "%s #{oredCriteria[%d].allCriteria[%d].value} and #{oredCriteria[%d].criteria[%d].secondValue}";
            parmPhrase2_th = "%s #{oredCriteria[%d].allCriteria[%d].value,typeHandler=%s} and #{oredCriteria[%d].criteria[%d].secondValue,typeHandler=%s}";
            parmPhrase3 = "#{oredCriteria[%d].allCriteria[%d].value[%d]}";
            parmPhrase3_th = "#{oredCriteria[%d].allCriteria[%d].value[%d],typeHandler=%s}";
        }
        
        StringBuilder sb = new StringBuilder();
        List<Criteria> oredCriteria = example.getOredCriteria();
        boolean firstCriteria = true;
        for (int i = 0; i < oredCriteria.size(); i++) {
            Criteria criteria = oredCriteria.get(i);
            if (criteria.isValid()) {
                if (firstCriteria) {
                    firstCriteria = false;
                } else {
                    sb.append(" or ");
                }
                
                sb.append('(');
                List<Criterion> criterions = criteria.getAllCriteria();
                boolean firstCriterion = true;
                for (int j = 0; j < criterions.size(); j++) {
                    Criterion criterion = criterions.get(j);
                    if (firstCriterion) {
                        firstCriterion = false;
                    } else {
                        sb.append(" and ");
                    }
                    
                    if (criterion.isNoValue()) {
                        sb.append(criterion.getCondition());
                    } else if (criterion.isSingleValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase1, criterion.getCondition(), i, j));
                        } else {
                            sb.append(String.format(parmPhrase1_th, criterion.getCondition(), i, j,criterion.getTypeHandler()));
                        }
                    } else if (criterion.isBetweenValue()) {
                        if (criterion.getTypeHandler() == null) {
                            sb.append(String.format(parmPhrase2, criterion.getCondition(), i, j, i, j));
                        } else {
                            sb.append(String.format(parmPhrase2_th, criterion.getCondition(), i, j, criterion.getTypeHandler(), i, j, criterion.getTypeHandler()));
                        }
                    } else if (criterion.isListValue()) {
                        sb.append(criterion.getCondition());
                        sb.append(" (");
                        List<?> listItems = (List<?>) criterion.getValue();
                        boolean comma = false;
                        for (int k = 0; k < listItems.size(); k++) {
                            if (comma) {
                                sb.append(", ");
                            } else {
                                comma = true;
                            }
                            if (criterion.getTypeHandler() == null) {
                                sb.append(String.format(parmPhrase3, i, j, k));
                            } else {
                                sb.append(String.format(parmPhrase3_th, i, j, k, criterion.getTypeHandler()));
                            }
                        }
                        sb.append(')');
                    }
                }
                sb.append(')');
            }
        }
        
        if (sb.length() > 0) {
            WHERE(sb.toString());
        }
    }
}